package com.hx.dao.impl;

import com.hx.dao.BaseDao;
import com.hx.dao.BizDao;
import com.hx.entity.Admin;
import com.hx.util.DateUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class BizDaoImpl extends BaseDao implements BizDao {
    @Override
    public boolean validateUser(String name, String pwd) {
        boolean flag=false;
        try {
            String sql="SELECT * FROM admin WHERE username=? AND PASSWORD=?";
            pst= getCon().prepareStatement(sql);
            pst.setString(1, name);
            pst.setString(2, pwd);
            //pst.execute();//正常执行的情况下，返回的是false,如果有异常的情况下返回的是true
            rs=pst.executeQuery();
            while(rs.next()){
                flag=true;
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            close(con,pst,rs);
        }
        return flag;
    }

    @Override
    public void Register(int aid,String username, String password, String phone) {
        String sql = "INSERT INTO admin VALUE(?,?,?,?,?)";
        PreparedStatement ps = null;
        int rs = 0;
        try{
            ps=getCon().prepareStatement(sql);
            ps.setInt(1,aid);
            ps.setString(2,username);
            ps.setString(3,password);
            ps.setString(4,phone);
            ps.setString(5, DateUtil.getDateStr(new Date()));
            rs = ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void updateUser(int aid, String password) {
        String sql = "UPDATE admin SET PASSWORD=?WHERE aid=?";
        PreparedStatement ps = null;
        try{
            ps=getCon().prepareStatement(sql);
            ps.setString(1,password);
            ps.setInt(2,aid);
            ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public Admin getAdminInfo(String username, String password) {

        Admin admin = new Admin();
        String sql = "select * from admin where username=? and password=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            ps=getCon().prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs=ps.executeQuery();
            if(rs.next()){
                admin.setAid(rs.getInt("aid"));
                admin.setUsername(rs.getString("username"));
                admin.setPassword(rs.getString("password"));
                admin.setPhone(rs.getString("phone"));
                admin.setCreated(rs.getString("created"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close(con,pst,rs);
        }
        return admin;
    }

    @Override
    public Admin get_AidInfo2(int aid) {
        Admin admin = new Admin();
        String sql = "select * from admin where aid=" + aid;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            ps=getCon().prepareStatement(sql);
            rs=ps.executeQuery();
            if(rs.next()){
                admin.setAid(rs.getInt("aid"));
                admin.setUsername(rs.getString("username"));
                admin.setPassword(rs.getString("password"));
                admin.setPhone(rs.getString("phone"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close(con,pst,rs);
        }
        return admin;
    }

    @Override
    public void deleteUser(int aid) {
        String sql = "delete from admin where aid=?";
        PreparedStatement ps = null;
        try {
            ps =getCon().prepareStatement(sql);
            ps.setInt(1, aid);
            ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
